# # 打开一个文件用于写入，如果文件不存在则创建
# with open('11-20.txt', 'w', encoding='utf-8') as file:
#     # 写入内容
#     file.write('徐光宗 男 19\n')

class TxtFileManager:
    def __init__(self, filename):
        self.filename = filename

    def create(self, content):
        """创建文件并写入初始内容"""
        with open(self.filename, 'w', encoding='utf-8') as file:
            file.write(content)

    def read(self):
        """读取文件内容"""
        try:
            with open(self.filename, 'r', encoding='utf-8') as file:
                return file.read()
        except FileNotFoundError:
            return "文件不存在。"

    def update(self, line_number, new_content):
        """更新指定行的内容"""
        lines = self.read().split('\n')
        if line_number < 1 or line_number > len(lines):
            return "行号超出范围。"
        lines[line_number - 1] = new_content
        with open(self.filename, 'w', encoding='utf-8') as file:
            file.write('\n'.join(lines))

    def delete(self, line_number):
        """删除指定行"""
        lines = self.read().split('\n')
        if line_number < 1 or line_number > len(lines):
            return "行号超出范围。"
        del lines[line_number - 1]
        with open(self.filename, 'w', encoding='utf-8') as file:
            file.write('\n'.join(lines))

    def append(self, content):
        """在文件末尾追加内容"""
        with open(self.filename, 'a', encoding='utf-8') as file:
            file.write(content + '\n')

# 使用示例
file_manager = TxtFileManager('example.txt')

# 创建文件
file_manager.create('Hello, World!\n')

# 读取文件
print(file_manager.read())

# 更新第二行
file_manager.update(1, 'Python\n')

# 追加内容
file_manager.append('Java\n')

# 删除第三行
# file_manager.delete(3)

# 再次读取文件查看结果
print(file_manager.read())